﻿Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid

Public Class ThuChiTienMat
    Private m_DoanhThu, m_PhanTramThue, m_TienThue, m_TongCong As Decimal
    Private m_MaKhachHang, m_TenKhachHang, m_DiaChi, m_MaSoThue, m_TaiKhoanThue, m_DienGiai As String
    Private m_Row_NganHang, m_Row_NghiepVu As DataRow
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.DMThoiGianTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMThoiGian)
        Me.DoiTuongTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DoiTuong)
        Me.TCTienTableAdapter.FillLoai_TCTien(Me.PCV_CLINICDataSet.TCTien, "1", CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue))
        Me.TCTienChiTietTableAdapter.Fill(Me.PCV_CLINICDataSet.TCTienChiTiet)
        Me.TCThueTableAdapter.Fill(Me.PCV_CLINICDataSet.TCThue)
        Me.TienMatTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.TienMat)
        SplashScreenManager.CloseForm()
    End Sub
    Public Overrides Sub setSubSau_TaoMoi()
        Me.SochungtuITextEdit.EditValue = Me.QueriesTableAdapter1.getSoChungTu_TCTien_NganHang("TCTM", CDate(TuNgayIDateEdit.EditValue), CDate(DenNgayIDateEdit.EditValue))
        Me.NgaychungtuIDateEdit.EditValue = Today
        Me.setMacDinhCotAn("ma", "TCTM" & System.Guid.NewGuid.ToString)
        Me.m_DoanhThu = 0
        Me.m_PhanTramThue = 0
        Me.m_TienThue = 0
        Me.m_TongCong = 0
        Me.m_MaKhachHang = ""
        Me.m_TenKhachHang = ""
        Me.m_DiaChi = ""
        Me.m_MaSoThue = ""
        Me.m_TaiKhoanThue = ""
        Me.m_DienGiai = ""
        Me.m_Row_NganHang = Nothing
        Me.m_Row_NghiepVu = Nothing
    End Sub
    Private Sub ThuChiTienMat_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DMThoiGianISearchLookupEdit.EditValue = "TN"
        loadDuLieu()
    End Sub
    Public Sub New()
        Me.IGridViewDsThamChieu1.setColumnCheck()
        Me.IGridViewDsThamChieu2.setColumnCheck()
        InitializeComponent()
        Me.setSoTabThamChieu(2)
        Me.setSoTab(2)
        Me.setObj(Me.PCV_CLINICDataSet, Me.PCV_CLINICDataSet.TCTien, Me.TCTienBindingSource, Me.TableAdapterManager, Me.TCTienTableAdapter, Me.ManghiepvuISearchLookupEdit, NoidungITextEdit)
        Me.setTab1(PCV_CLINICDataSet.TCTienChiTiet, FKTCTienChiTiema47A6A41BBindingSource, TCTienChiTietTableAdapter)
        Me.setTab2(Me.PCV_CLINICDataSet.TCThue, Me.FKTCThuema46B27FE2BindingSource, Me.TCThueTableAdapter)
    End Sub
    Private Sub MadonviISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MadonviISearchLookupEdit.EditValueChanging
        Me.TendonviITextEdit.EditValue = Me.MadonviISearchLookupEdit.getRows.Item("ten").ToString
        Me.DiachiITextEdit.EditValue = Me.MadonviISearchLookupEdit.getRows.Item("diachi").ToString
        m_Row_NghiepVu = getRow(Me.PCV_CLINICDataSet_Don.TienMat, TienMatBindingSource, "ma", Me.ManghiepvuISearchLookupEdit.EditValue)
        Me.NoidungITextEdit.EditValue = m_Row_NghiepVu.Item("diengiai").ToString & " " & TendonviITextEdit.EditValue
    End Sub
    Private Sub RepositoryItemISearchLookUpEdit_DoiTuong_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemISearchLookUpEdit_DoiTuong.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        IGridViewChiTiet01.SetFocusedRowCellValue("madonvi", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewChiTiet01.SetFocusedRowCellValue("tendonvi", editor.Properties.View.GetRowCellValue(rowHandle, "ten"))
        IGridViewChiTiet01.SetFocusedRowCellValue("diachidonvi", editor.Properties.View.GetRowCellValue(rowHandle, "diachi"))
    End Sub
    Private Sub IGridViewChiTiet01_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewChiTiet01.CellValueChanged
        If Me.IGridViewChiTiet01.FocusedColumn.FieldName = "tienvnd" Then
            Me.IGridViewChiTiet01.setUpdateData()
            Me.TienvndITextEdit.EditValue = Me.IGridViewChiTiet01.getSummaryColumn("tienvnd")
        End If
    End Sub
    Private Sub IGridViewChiTiet01_InitNewRow(sender As Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet01.InitNewRow
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
    End Sub
    Private Sub DMThoiGianISearchLookupEdit_EditValueChanged(sender As Object, e As EventArgs) Handles DMThoiGianISearchLookupEdit.EditValueChanged
        setDMThoiGian(Me.DMThoiGianISearchLookupEdit, TuNgayIDateEdit, DenNgayIDateEdit)
        ReLoad()
    End Sub
    Private Sub ReLoad()
        Dim position As Integer = Me.TCTienBindingSource.Position
        loadDuLieu()
        Me.TCTienBindingSource.Position = position
    End Sub
    Protected Overrides Sub BarButtonItemLamTuoi_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        ReLoad()
    End Sub
    Private Sub ChuyểnDữLiệuThuếToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ChuyểnDữLiệuThuếToolStripMenuItem.Click
        If Me.getState <> 0 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn chuyển dữ liệu thuế không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewChiTiet01.Columns("sohoadon").GroupIndex = 0
                Me.IGridViewChiTiet01.Columns("ngayhoadon").GroupIndex = 1
                For index = 0 To Me.IGridViewChiTiet01.RowCount - 1
                    Me.IGridViewChiTiet01.MoveNext()
                Next
                Me.IGridViewChiTiet01.ClearGrouping()
                Me.TabbedControlGroupChiTiet.SelectedTabPage = Me.TabbedControlGroupChiTiet.TabPages(1) 'chọn tab thue
            End If
        End If
    End Sub
    Private Sub IGridViewChiTiet02_InitNewRow(sender As Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet02.InitNewRow
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("manghiepvu", Me.ManghiepvuISearchLookupEdit.EditValue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("sochungtu", Me.SochungtuITextEdit.EditValue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuIDateEdit.EditValue))
        '==================================================================
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("madonvi", Me.m_MaKhachHang)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("tendonvi", Me.m_TenKhachHang)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("diachi", Me.m_DiaChi)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("masothue", Me.m_MaSoThue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("doanhthu", Me.m_DoanhThu)
        Me.m_PhanTramThue = CInt(getRow(Me.PCV_CLINICDataSet_Don.TienMat, TienMatBindingSource, "ma", Me.ManghiepvuISearchLookupEdit.EditValue).Item("tylevat"))
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("phantramthue", Me.m_PhanTramThue) 'phân biệt chữ hoa thường
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("tienthue", Me.m_TienThue)
        Me.IGridViewChiTiet02.SetFocusedRowCellValue("tongcong", Me.m_TongCong)
    End Sub
    Private Sub IGridViewChiTiet01_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles IGridViewChiTiet01.FocusedRowChanged
        Dim currentView As GridView = CType(sender, GridView)
        Dim groupRowHandle As Integer
        If (e.FocusedRowHandle >= 0) Then
            groupRowHandle = currentView.GetParentRowHandle(e.FocusedRowHandle)
        Else
            groupRowHandle = e.FocusedRowHandle
        End If
        If Not currentView.IsGroupRow(groupRowHandle) Then Return
        Dim ht As Hashtable = currentView.GetGroupSummaryValues(groupRowHandle)
        Dim entry As DictionaryEntry
        Me.TCTienBindingSource.EndEdit()
        For Each entry In ht
            Dim sumItem As GridGroupSummaryItem = entry.Key
            Dim val As Object = entry.Value
            Try
                Me.m_MaKhachHang = Me.IGridViewChiTiet01.GetGroupRowValue(Me.IGridViewChiTiet01.FocusedRowHandle, Me.IGridViewChiTiet01.Columns("madonvi"))
                Me.m_TenKhachHang = Me.IGridViewChiTiet01.GetGroupRowValue(Me.IGridViewChiTiet01.FocusedRowHandle, Me.IGridViewChiTiet01.Columns("tendonvi"))
                Me.m_DiaChi = Me.IGridViewChiTiet01.GetGroupRowValue(Me.IGridViewChiTiet01.FocusedRowHandle, Me.IGridViewChiTiet01.Columns("diachidonvi"))
                Me.m_MaSoThue = ""
                Me.m_DoanhThu = CDec(val)
                Me.m_PhanTramThue = CInt(getRow(Me.PCV_CLINICDataSet_Don.TienMat, TienMatBindingSource, "ma", Me.ManghiepvuISearchLookupEdit.EditValue).Item("tylevat"))
                Me.m_TienThue = m_PhanTramThue * Me.m_DoanhThu / 100
                Me.m_TongCong = Me.m_DoanhThu + m_TienThue
                Me.IGridViewChiTiet02.AddNewRow()
            Catch ex As Exception
                MessageBox.Show("IGridViewChiTiet01_FocusedRowChanged" & ex.Message)
            End Try
        Next
    End Sub
    Private Shadows Sub BarButtonItemBaoCao_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemBaoCao.ItemClick
        If Not IsDBNull(Me.ManghiepvuISearchLookupEdit.EditValue) AndAlso Not IsNothing(Me.ManghiepvuISearchLookupEdit.EditValue) Then
            If Me.ManghiepvuISearchLookupEdit.EditValue.ToString = "CTM01" Then
                Dim report As Report_PhieuChiTienMat = New Report_PhieuChiTienMat
                report.TcTienTableAdapter1.FillMa_TCTien(report.PcV_CLINICDataSet1.TCTien, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
                report.NgayThangNam.Value = "Ngày " & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "dd") & " Tháng " & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "MM") & " Năm " & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "yyyy")
                report.TienBangChu.Value = NumToText(Me.TienvndITextEdit.EditValue.ToString) & " đồng."
                report.ShowRibbonPreview()
            ElseIf Me.ManghiepvuISearchLookupEdit.EditValue.ToString = "TTM01" Then
                Dim report As Report_PhieuThuTienMat = New Report_PhieuThuTienMat
                report.TcTienTableAdapter1.FillMa_TCTien(report.PcV_CLINICDataSet1.TCTien, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
                report.NgayThangNam.Value = "Ngày " & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "dd") & " Tháng " & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "MM") & " Năm " & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "yyyy")
                report.TienBangChu.Value = NumToText(Me.TienvndITextEdit.EditValue.ToString) & " đồng."
                report.ShowRibbonPreview()
            End If
        End If
    End Sub
End Class
